﻿using CPF.Input;
using ScottPlot.Interactivity;
using MouseButton = CPF.Input.MouseButton;

namespace ScottPlot.CPF;

internal static class CPFPlotExtensions
{
    internal static void ProcessMouseDown(this UserInputProcessor processor, MouseButtonEventArgs e)
    {
        Pixel pixel = e.Pixel();
        IUserAction action = e.MouseButton switch
        {
            MouseButton.Left => new Interactivity.UserActions.LeftMouseDown(pixel),
            MouseButton.Middle => new Interactivity.UserActions.MiddleMouseDown(pixel),
            MouseButton.Right => new Interactivity.UserActions.RightMouseDown(pixel),
            _ => new Interactivity.UserActions.Unknown("mouse down", "pressed"),
        };

        processor.Process(action);
    }

    internal static void ProcessMouseUp(this UserInputProcessor processor, MouseButtonEventArgs e)
    {
        Pixel pixel = e.Pixel();
        IUserAction action = e.MouseButton switch
        {
            MouseButton.Left => new Interactivity.UserActions.LeftMouseUp(pixel),
            MouseButton.Middle => new Interactivity.UserActions.MiddleMouseUp(pixel),
            MouseButton.Right => new Interactivity.UserActions.RightMouseUp(pixel),
            _ => new Interactivity.UserActions.Unknown("mouse down", "released"),
        };

        processor.Process(action);
    }

    public static void ProcessMouseMove(this UserInputProcessor processor, MouseEventArgs e)
    {
        Pixel pixel = e.Pixel();
        IUserAction action = new Interactivity.UserActions.MouseMove(pixel);
        processor.Process(action);
    }

    public static void ProcessMouseWheel(this UserInputProcessor processor, MouseWheelEventArgs e)
    {
        Pixel pixel = e.Pixel();

        IUserAction action = e.Delta.Y > 0
            ? new Interactivity.UserActions.MouseWheelUp(pixel)
            : new Interactivity.UserActions.MouseWheelDown(pixel);

        processor.Process(action);
    }

    public static void ProcessKeyDown(this UserInputProcessor processor, KeyEventArgs e)
    {
        Key key = e.ToKey();
        IUserAction action = new Interactivity.UserActions.KeyDown(key);
        processor.Process(action);
    }

    internal static void ProcessKeyUp(this UserInputProcessor processor, KeyEventArgs e)
    {
        Key key = e.ToKey();
        IUserAction action = new Interactivity.UserActions.KeyUp(key);
        processor.Process(action);
    }

    public static Key ToKey(this KeyEventArgs e)
    {
        return e.Key switch
        {
            Keys.LeftCtrl => StandardKeys.Control,
            Keys.RightCtrl => StandardKeys.Control,
            Keys.LeftAlt => StandardKeys.Alt,
            Keys.RightAlt => StandardKeys.Alt,
            Keys.LeftShift => StandardKeys.Shift,
            Keys.RightShift => StandardKeys.Shift,
            _ => new Key(e.ToString() ?? string.Empty),
        };
    }

    internal static Pixel Pixel(this MouseEventArgs e)
    {
        double x = e.Location.X;
        double y = e.Location.Y;
        return new Pixel((float)x, (float)y);
    }

}
